package test;

import java.util.HashMap;  
import java.util.Map;
import java.util.Scanner;  
  
public class hh {       
    private Map<String, Map<Integer, Integer>> index;  
    private Map<Integer, Integer> subIndex;        
    public void createIndex(String word) {     	
        index = new HashMap<String, Map<Integer, Integer>>();
        int line = 1;  
        while ((word) != null) {              
            if (!index.containsKey(word)) {  
                subIndex = new HashMap<Integer, Integer>();  
                subIndex.put(line, 1);  
                index.put(word, subIndex);  
            } 
            else {  
                subIndex = index.get(word);  
                if (subIndex.containsKey(line)) {  
                    int count = subIndex.get(line);  
                    subIndex.put(line, count+1);  
                 } 
                else  subIndex.put(line, 1);  
            }
            if(word.equals("!!!!!"))  break;
            line++;  
        }  
       
     }     
    public void find(String word) {  
        StringBuilder sb = new StringBuilder();  
        if (index.containsKey(word)) {  
                sb.append("word: " + word + " in ");  
                Map<Integer, Integer> temp = index.get(word);  
                for (Map.Entry<Integer, Integer> e : temp.entrySet()) {  
                    sb.append("line " + e.getKey() + " [" + e.getValue() + "] , ");   
                }  
            } else {  
                sb.append("word: " + word + " not found");  
            }  
            System.out.println(sb);  
      }  
      
      
    public static void main(String[] args) {  
        hh index = new hh();  
        index.createIndex("I love Shanghai today");  
        index.find("love");  
    }  
}  